import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { HttpExceptionFilter } from './common/http-exception.filter';
import { ResponseInterceptor } from './common/response-interceptor';
import { ValidationPipe } from './module/utils/validatePipe';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    cors: true,
  });

  // 给所有请求加上前缀api
  app.setGlobalPrefix('api');
  // 注册网络异常处理器
  app.useGlobalFilters(new HttpExceptionFilter());
  // 注册全局响应拦截器
  app.useGlobalInterceptors(new ResponseInterceptor());
  // 注册全局规则验证
  app.useGlobalPipes(new ValidationPipe());

  // 定义全局上传文件路径
  app.useStaticAssets(join(__dirname, '..', 'public'), {
    prefix: '/vehicle/',
  });

  // 配置 admin swagger 文档
  const swaggerAdminOption = new DocumentBuilder()
    .setTitle('车主帮后台管理系统接口文档')
    .addBearerAuth()
    .setDescription(
      '该文档用于罗春毕业设计 《基于Vue3+nest+umi车主帮平台设计与实现》 后台管理系统的 api 接口',
    )
    .setVersion('1.0')
    .build();

  const adminDocument = SwaggerModule.createDocument(app, swaggerAdminOption);
  SwaggerModule.setup('/doc/admin', app, adminDocument);

  await app.listen(3000);
}
bootstrap();
